package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SearchInfo
 */
public class SearchInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String rid = request.getParameter("rid");
		if (rid == null)
			rid="-1";
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		try {
			Connection con = dbOperator.BasicOperation.getConnection();
			ResultSet rs;
			rs = dbOperator.AdvancedOperation.getReaderInfo(con, rid, "uid");
			if(rs.next()) {
				out.println("<p>UID:	"+ rs.getInt("uid") + "</p>");
				out.println("<p>Email:	" + rs.getString("Email") + "</p>");
				out.println("<p>phoneNumber:	" + rs.getString("phoneNumber") + "</p>");
			}
			out.println("<b>borrow history:</b>");
			rs = dbOperator.AdvancedOperation.getBorrowList(con, rid, "uid");
			double fine=0;
			String datestr=null;
			while(rs.next()) {
				if(rs.getString("status").equals("borrowed")) {
				out.println("<p>");
				out.print("<b>	BID:	</b>"+rs.getString("bid"));
				datestr=rs.getString("borrowDate");
				out.print("<b>	borrowDate:	</b>"+datestr);
				out.print("<b>	status:	</b>borrowed");
				fine=dbOperator.AdvancedOperation.getExpiredFine(con, datestr);
				if(fine>0)out.print("(expired fine="+fine+")");
				out.println("</p>");
				}
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
